package cn.comon.app.scan.service.impl;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import cn.comon.app.scan.component.SysConfigBean;

public class ThreadPoolService {
	
	private static Map<String, ThreadPoolExecutor> poolMaps = new ConcurrentHashMap<String, ThreadPoolExecutor>();
	
	public static ThreadPoolExecutor getTheadPool(String poolName) {
		
		if(poolMaps.containsKey(poolName)) {
			return poolMaps.get(poolName);
		}
		int poo_size = Integer.valueOf(SysConfigBean.getProperty("app.download.threadpool.size"));
		ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * poo_size);
		poolMaps.put(poolName, executor);
		return executor;
	}
	public static ThreadPoolExecutor getTheadPool(String poolName, int poolSize) {
		
		if(poolMaps.containsKey(poolName)) {
			return poolMaps.get(poolName);
		}
		ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(poolSize);
		poolMaps.put(poolName, executor);
		return executor;
	}

	public static void removeTheadPool(String poolName) {
		if(poolMaps.containsKey(poolName)) {
			poolMaps.remove(poolName);
		}
	}
	
	public static int getTaskQueueSize(String poolName) {
		if(poolMaps.containsKey(poolName)) {
			return getThreadPoolQueueSize(poolMaps.get(poolName));
		} else {
			return 0;
		}
	}
	
	private static int getThreadPoolQueueSize(ThreadPoolExecutor executor) {
		return executor.getQueue().size();
	}
	public static void main(String[] args) {
		ThreadPoolExecutor a = getTheadPool("as");
		System.out.println(a.getQueue().size());
	}
	
	
	
}
